﻿using System;
using System.Linq;
using System.Web.Mvc;
using Lbb.Localization;
using Lbb.Web.Mvc;
using Lbb.URating.Controllers;
using Lbb.URating.DL;
using Lbb.URating.Localization;
using Lbb.URating.Models;

namespace Lbb.URating.Areas.admin.Controllers
{
    /// <summary>
    /// 评分项控制器
    /// </summary>
    /// <author>Chiron</author>
    public class U_ClickRatingController : URatingControllerBase
    {
        #region Fields
        private const string _preKey = "U_ClickRating_";
        private readonly static string[] _listKeys = URatingControllerBase.listKeys.Union(new[] { 
            "categoryIds", "clickIds" ,"objIds","minClickCount","maxClickCount","minAverageRating","maxAverageRating"
        }).ToArray();
        #endregion Fields

        #region Pages
        /// <summary>
        /// 列表页面
        /// </summary>
        public ActionResult Index(int? pageNumber, int? pageSize, string keyword, string orderby,
            string categoryIds, string clickIds, string objIds, int? minClickCount, int? maxClickCount, decimal? minAverageRating, decimal? maxAverageRating)
        {
            Response.Expires = 0;
            CacheArgs(_preKey, _listKeys);
            var model = new U_ClickRatingDataList();
            model.FillData(U_ClickRatingRepository.Find(model.Keyword, model.CategoryIds, model.ClickIds, model.ObjtypeIds, model.MinClickCount, model.MaxClickCount, model.MinAverageRating, model.MaxAverageRating));
            return View(IsRequestPartial ? "List" : "Index", model);
        }
        /// <summary>
        /// 返回到上次查看的列表页
        /// </summary>
        public ActionResult RedirectToIndex()
        {
            return RedirectToAction("Index", GetCacheArgs(_preKey, _listKeys));
        }
        /// <summary>
        /// 添加/编辑页面
        /// </summary>
        public ActionResult Edit(Guid? id, Guid? clickId, string objId)
        {
            var model = U_ClickRatingRepository.Get(id) ?? new U_ClickRating
            {
                ClickID = clickId ?? Guid.Empty,
                ObjectID = objId
            };
            return View(model);
        }
        /// <summary>
        /// 保存评分分类
        /// </summary>
        [HttpPost]
        public ActionResult Edit(U_ClickRating model)
        {
            try
            {
                if (U_ClickRepository.Get(model.ClickID) == null) ModelState.AddModelError("ClickID", string.Format(OperatorRes.NotFindSelectedObj, U_ClickRatingRes.ClickID));
                if (ModelState.IsValid)
                {
                    if (model.RatingID == Guid.Empty)
                    {
                        U_ClickRatingRepository.Add(model);
                    }
                    else
                    {
                        U_ClickRatingRepository.Update(model);
                    }
                    U_ClickRatingRepository.Save();
                    return RedirectToIndex();
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);

            }
            return View(model);
        }
        /// <summary>
        /// 删除评分分类
        /// </summary>
        [HttpPost]
        public ActionResult Delete(Guid[] id)
        {
            var result = new JsonResponse();

            if (IsValidValue(ref id))
            {
                try
                {
                    U_ClickRatingRepository.DeleteRange(U_ClickRatingRepository.Find(d => id.Contains(d.RatingID)));
                    U_ClickRatingRepository.Save();
                }
                catch (Exception ex)
                {
                    result.AppendMessage(ex);
                }
            }
            else
            {
                result.AppendMessage(string.Format(OperatorRes.PleaseSelectObj, OperatorRes.Delete, U_ClickRatingRes.ClassName));
            }

            result.success = string.IsNullOrEmpty(result.message);
            return Json(result);
        }
        #endregion Pages


    }
}
